Public Class CNguoiSuDung
    Private _user_id As Integer
    Private _loginname As String
    Private _pass As String
    Private _fullname As String
    Private _nhom_id As Integer
    Private _ghichu As String
    Private _isAdmin As Boolean
    Private _isHide As Boolean

    Public Sub New()
        _isAdmin = False
        _isHide = False
        _nhom_id = -1
    End Sub

#Region "Cac ham property"
    Public Property User_ID() As Integer
        Get
            Return _user_id
        End Get
        Set(ByVal Value As Integer)
            _user_id = Value
        End Set
    End Property

    Public Property LoginName() As String
        Get
            Return _loginname
        End Get
        Set(ByVal Value As String)
            _loginname = Value
        End Set
    End Property

    Public Property Pass() As String
        Get
            Return _pass
        End Get
        Set(ByVal Value As String)
            _pass = Value
        End Set
    End Property

    Public Property FullName() As String
        Get
            Return _fullname
        End Get
        Set(ByVal Value As String)
            _fullname = Value
        End Set
    End Property

    Public Property Nhom_ID() As Integer
        Get
            Return _nhom_id
        End Get
        Set(ByVal Value As Integer)
            _nhom_id = Value
        End Set
    End Property

    Public Property GhiChu() As String
        Get
            Return _ghichu
        End Get
        Set(ByVal Value As String)
            _ghichu = Value
        End Set
    End Property

    Public Property IsAdmin() As Boolean
        Get
            Return _isAdmin
        End Get
        Set(ByVal value As Boolean)
            _isAdmin = value
        End Set
    End Property

    Public Property IsHide() As Boolean
        Get
            Return _isHide
        End Get
        Set(ByVal value As Boolean)
            _isHide = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return _loginname
    End Function
#End Region

    Public Function CapNhat(ByVal trangThaiAdminSau As Boolean) As Boolean
        Dim kq As Boolean = True
        Dim cauLenh As String = ""

        'thuc hien kiem tra trang thai Admin luc dau va trang thai Admin luc sau de cap nhat
        'If (_isAdmin = True And trangThaiAdminSau = False) Then
        '    'tuc la tu vi tri la Admin chuyen thanh khong phai la Admin
        '    cauLenh &= " exec sp_dropsrvrolemember '" & _loginname & "', 'sysadmin'"
        'End If
        'If (_isAdmin = False And trangThaiAdminSau = True) Then
        '    'duoc cap quyen Admin
        '    cauLenh &= " exec sp_addsrvrolemember '" & _loginname & "', 'sysadmin'"
        'End If

        'Dim strIsAdmin As String = "select IS_SRVROLEMEMBER('sysadmin','" + _loginname + "')"
        'If CInt(XL_DULIEU.LayGiaTri(strIsAdmin)) = 1 And trangThaiAdminSau = False Then
        '    cauLenh &= " exec sp_dropsrvrolemember '" & _loginname & "', 'sysadmin'"
        'ElseIf CInt(XL_DULIEU.LayGiaTri(strIsAdmin)) = 0 And trangThaiAdminSau = True Then
        '    cauLenh &= " exec sp_addsrvrolemember '" & _loginname & "', 'sysadmin'"
        'End If

        '_isAdmin = trangThaiAdminSau
        'thuc hien cau lenh cap nhat
        Dim strNhomId As String = _nhom_id.ToString()
        If (_nhom_id = -1) Then
            strNhomId = "null"
        End If
        cauLenh &= " update " + s_tblNguoiDung + " set"
        cauLenh &= " fullname =N'" & _fullname & "' "
        cauLenh &= ",nhom_id =" & strNhomId
        cauLenh &= ",ghichu = N'" & _ghichu & "'"
        If (trangThaiAdminSau = True) Then
            cauLenh &= ",isAdmin = 1"
        Else
            cauLenh &= ",isAdmin = 0"
        End If
        cauLenh &= " where user_id =" & _user_id.ToString()
        'thuc hien cau lenh
        If (XL_DULIEU.ThucHienLenh(cauLenh) = False) Then
            kq = False
        End If
        Return kq
    End Function

    Public Function themNSD() As Integer
        Dim kq As Integer
        kq = XL_DULIEU.ThemNSD(_loginname, _pass, _fullname, _ghichu)
        Return kq
    End Function

    Public Function SetHideNSD(ByVal isHide As Boolean) As Boolean
        'xoa trong co so du lieu
        Dim cauLenh As String

        If isHide = True Then
            cauLenh = "update " + s_tblNguoiDung + " set isHide = 1 where User_ID = " & _user_id
        Else
            cauLenh = "update " + s_tblNguoiDung + " set isHide = 0 where User_ID = " & _user_id
        End If

        If (XL_DULIEU.ThucHienLenh(cauLenh) = False) Then
            Return False
        End If
        Return True
    End Function

    Public Shared Function XoaNSD(ByVal user_id As Integer) As Boolean
        'xoa trong co so du lieu
        Dim cauLenh As String = ""
        'cauLenh = " delete from tblSYSVAR where user_id = " & user_id
        cauLenh = " delete from " + s_tblNguoiDung + " where user_id = " & user_id

        If (XL_DULIEU.ThucHienLenh(cauLenh)) Then
            Return True
        End If
        Return False
    End Function

    'Public Shared Function GetUserByName(ByVal pLoginName As String) As ArrayList
    '    Dim aryItem As New ArrayList
    '    Dim strSQL As String

    '    strSQL = "SELECT * FROM tblUser where loginname = N'" + pLoginName + "'"

    '    Try
    '        Dim db As DataTable = New DataTable()
    '        XL_DULIEU.LayDuLieuRaBang(strSQL, db)
    '        Dim oUser As New CNguoiSuDung
    '        With oUser
    '            .User_ID = Integer.Parse(db.Rows(0).Item(0).ToString())
    '            .LoginName = db.Rows(0).Item(1).ToString()
    '            .Pass = db.Rows(0).Item(2).ToString()
    '            .FullName = db.Rows(0).Item(3).ToString()
    '            .Nhom_ID = Integer.Parse(db.Rows(0).Item(4).ToString())
    '            .IsAdmin = Boolean.Parse(db.Rows(0).Item(5).ToString())
    '            .IsHide = Boolean.Parse(db.Rows(0).Item(6).ToString())
    '            .GhiChu = db.Rows(0).Item(7).ToString()
    '        End With
    '        aryItem.Add(oUser)
    '    Catch ex As System.Exception
    '        Throw ex
    '    End Try
    '    Return aryItem
    'End Function
End Class
